// pages/userdetail/userdetail.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:"",
    mobilePhone:"",
    guiShu:"",
    gslist:[],
    index:0,
    selectShow: false,//控制下拉列表的显示隐藏，false隐藏、true显示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var i=this
    wx.showLoading({
      title: "加载中..."
    }),
    wx.request({
      url: app.resPath + "json/guishu.json",//获取远程文件
      method: "GET", 
      success: (res)=> {       
       console.log(res);
        this.setData({
          gslist: res.data.gslist
        });
        // console.log(999,res.data.gslist);   
      },
      fail (e) {
        wx.showToast({
          title: '错误',
      })
      }
    }),
    wx.request({
      url: app.domain + "ssm/appWebServ/userInfo",
      data: {
        openid: app.openid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res)
        i.setData({
          username: res.data.info.real_name,
          mobilePhone: res.data.info.contact_phone,
          guiShu:res.data.info.rmk,
          index:i.data.gslist.indexOf(res.data.info.rmk)>0?i.data.gslist.indexOf(res.data.info.rmk):0
        });
      },
      complete (r) {
          wx.hideLoading()
      }
    })
  },
  onUserNameInput(e) {
    this.setData({
      username: e.detail.value
    })
},
onMobilePhoneInput(e) {
    this.setData({
      mobilePhone: e.detail.value
    })       
},
onGuiShuInput(e) {
  this.setData({
    guiShu: e.detail.value
  })       
},
// 点击下拉显示框
selectTap() {
  this.setData({
    selectShow: !this.data.selectShow
  });
},
// 点击下拉列表
optionTap(e) {
  let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
  this.setData({
    index: Index,
    selectShow: !this.data.selectShow
  });
},
save(e) {
    var s = this;
    if (!this.data.username) {
        wx.showToast({
            title: "请输入用户姓名",
            icon: "error"
        })
        return;
    }   
    if (!this.data.mobilePhone){
         wx.showToast({
            title: "请输入手机号码",
            icon: "error"
        })
        return;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobilePhone.length !=11||!myreg.test(this.data.mobilePhone)){
         wx.showToast({
            title: "手机号码错误",
            icon: "error"
        })
        return;
    }
    // if (!this.data.guiShu){
    //   wx.showToast({
    //      title: "请输入归属支行",
    //      icon: "error"
    //  })
    //  return;
    // }
    if (this.data.index==0){
      wx.showToast({
         title: "请选择归属支行",
         icon: "error"
     })
     return;
    }
    wx.showLoading({
        title: "保存中..."
    });
    // console.log(this.data.index)
    // console.log(this.data.guiShu)
    wx.request({
      url: app.domain + "ssm/appWebServ/saveUserInfo",
      data: {
        realname: this.data.username,
        phone: this.data.mobilePhone,
        groupid: "",
        groupname: this.data.gslist[this.data.index],
        openid: app.openid,
      },
      success: (res)=>  {
          console.log(res)
          if("0"==res.data.respCode){
              wx.showToast({title: "保存成功"});
          }else{
              wx.showToast({title: "保存失败",icon: 'error'});
          }
          
      },
      // complete: ()=> {
      //     wx.hideLoading();
      // }
    })
    
},
})